package com.lt.flashcard.model.service;

import java.util.List;

import org.bson.types.ObjectId;

import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import com.lt.flashcard.model.entity.Content;
import com.lt.flashcard.model.entity.ContentCategory;

/**
 * 
 * @author ocsen
 *
 */
public class ContentService {

	private BasicDAO<Content, ObjectId> _contentDAO = null;
	private BasicDAO<Content, ObjectId> getContentDAO() {
		if(_contentDAO == null)
			_contentDAO = new BasicDAO<Content, ObjectId>(Content.class, Repository.getInstance().getDatastore());
		return _contentDAO;
	}
	
	private BasicDAO<ContentCategory, ObjectId> _contentcategoryDAO = null;
	private BasicDAO<ContentCategory, ObjectId> getContentCategoryDAO() {
		if(_contentcategoryDAO == null)
			_contentcategoryDAO = new BasicDAO<ContentCategory, ObjectId>(ContentCategory.class, Repository.getInstance().getDatastore());
		return _contentcategoryDAO;
	}
	
	/**
	 * Save a content
	 * @param content
	 */
	public void saveContent(Content content) {
		getContentDAO().save(content);
	}
	
	/**
	 * Delete a content
	 * @param content
	 */
	public void deleteContent(Content content) {
		getContentDAO().delete(content);
	}
	
	/**
	 * Find a content by id
	 * @param id
	 * @return
	 */
	public Content getContent(String id) {
		return getContentDAO().get(new ObjectId(id));
	}
	
	/**
	 * Save a category
	 * @param category
	 */
	public void saveCategory(ContentCategory category) {
		getContentCategoryDAO().save(category);
	}
	
	/**
	 * Delete a category
	 * @param category
	 */
	public void deleteCategory(ContentCategory category) {
		BasicDAO<ContentCategory, ObjectId> dao = getContentCategoryDAO();
		if(category.getChild().size() > 0) {
			for(ContentCategory cat : category.getChild())
				dao.delete(cat);
		}
		dao.delete(category);
	}
	
	/**
	 * Find category by id
	 * @param id
	 * @return
	 */
	public ContentCategory getCategory(String id) {
		return getContentCategoryDAO().get(new ObjectId(id));
	}
	
	/**
	 * Get all categories from system
	 * @return
	 */
	public List<ContentCategory> getAllCategories() {
		BasicDAO<ContentCategory, ObjectId> dao = getContentCategoryDAO();
		Query<ContentCategory> query = dao.createQuery();
		query.field("parent").doesNotExist();
		query.order("-order");
		return query.asList();
	}
	
	/**
	 * Get number of contents
	 * @param category
	 * @return
	 */
	public long countNumberOfContent(ContentCategory category) {
		BasicDAO<Content, ObjectId> dao = getContentDAO();
		Query<Content> query = dao.createQuery();
		query.field("category").equal(category);
		return query.countAll();
	}
	
	/**
	 * Get all static contents in categories
	 * @param category
	 * @return
	 */
	public List<Content> getContentsOfCategory(ContentCategory category) {
		BasicDAO<Content, ObjectId> dao = getContentDAO();
		Query<Content> query = dao.createQuery();
		query.field("category").equal(category);
		query.order("-order");
		return query.asList();
	}
	
	/**
	 * Get all contents in system
	 * @return
	 */
	public List<Content> getAllContents() {
		BasicDAO<Content, ObjectId> dao = getContentDAO();
		Query<Content> query = dao.createQuery();
		return query.asList();
	}
}
